var hostList=[];
var windowHeight=$(window).height();
$(function () {
    var $hostObj = $('#hostCode');
    Ajax({//主机
        url: contextPath + '/console/host/getAllHost',
        isMask: true,
        timeout: 0,
        data: {},
        success: function (data) {
            hostList = data;
            $hostObj.combobox({
                data:hostList,
                valueField: 'hostCode',
                textField: 'hostName',
                editable: false,
                panelHeight:　'auto',
                prompt:　'----请选择----',
                formatter: function(data){
                    if(data.hostName === '----请选择----'){
                        return data.hostName;

                    }
                    return data.hostName+'['+data.hostIp+']';
                },
                onLoadSuccess: function (data) {
                    // 缓存上游系统列表，模块datagrid初始化时会用到该数据
                    hostList = data;

                },
                onLoadError: function () {

                },
                onShowPanel: function () {
                    // 动态调整下拉高度
                    if (hostList && hostList.length > 10) {
                        $(this).combobox('panel').height(285);
                    }
                },
                onChange: function (value) {
                }
            });
            $("#search_hostCode").combobox({
                data:hostList,
                valueField: 'hostCode',
                textField: 'hostName',
                editable: false,
                panelHeight:　'auto',
                onLoadSuccess: function (data) {
                },
                onLoadError: function () {

                },
                onShowPanel: function () {
                    // 动态调整下拉高度
                    if (hostList && hostList.length > 10) {
                        $(this).combobox('panel').height(285);
                    }
                },
                onChange: function (value) {
                }
            });
        }
    });
});

function getFileListTree(path,url,showFile,setPath) {
    // var path =  $("#path").val()
    // var url =  $("#url").val()
    // var showFile = $("#showFile").combobox('getText');
    // console.log( path+ url+showFile)
    // console.log("path"+path+"url"+url+"showfile"+showFile)
    var file = {
        attributes: {
            type: path,
            //如填folder或file则直接加载当前目录下的文件夹或文件，不显示根目录。填写中间件名称则显示根目录，根目录为type名称；
            path: path,
            //当前中间件布属路径
            url: url
            //URL:地址；http://主机ip:+主机监控程序端口/ctrl ,当前中间件布属的主机ip：端口/ctrl;
        },
    };

    $('#orgInfoTree').tree({
        url: contextPath + "/console/file/fileList?currentType=" + file.attributes.type + '&currentPath=' + file.attributes.path
        + "&currentUrl=" + file.attributes.url + "&currentShowFile=" + showFile,

        onLoadSuccess: function (node, data) {

            if (data.length === 0) {
                apexLayer.Msg("暂无数据");

            }
            else {
                var t = $(this);
                if(data[0].attributes.type==='top')//第一次加载全部加载
                {
                    if (data[0].state == 'closed') {
                        t.tree('expandAll');
                    }
                }
                limitHeight();//展开节点时限制高度
            }

        },

        //异步加载
        onBeforeExpand: function (node) {
            if (node) {
                $('#orgInfoTree').tree('options').url = contextPath + "/console/file/fileList?currentType=" + node.attributes.type + '&currentPath=' + node.attributes.path
                    + "&currentUrl=" + node.attributes.url+"&currentShowFile="+showFile;
            }

        },
        //双击选中
        onDblClick:function (node) {//双击选择文件，打开文件夹
            if(showFile==="true"){
                $(this).tree(node.state === 'closed' ? 'expand' : 'collapse', node.target);
                node.state = node.state === 'closed' ? 'open' : 'closed';
                if(node.attributes.type==="file")
                {

                    setPath(node)
                    $("#add-path").dialog("close")
                }
            }
            else{
                setPath(node)
                $("#add-path").dialog("close")
            }

        },
        onContextMenu: function(e, node) {
            e.preventDefault();
            // 查找节点
            $('#orgInfoTree').tree('select', node.target);
            // 显示快捷菜单
            $('#mm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        },
        onExpand:function () {
            limitHeight();//展开节点时限制高度
        }
        // 格式化数据
        // formatter:function(node){
        //     var s = node.text;
        //     if (node.children){
        //         s += '&nbsp;<span style=\'color:blue\'>(' + node.children.length + ')</span>';
        //     }
        //     return s;
        // }
    });
    var root=$("#orgInfoTree").tree("getRoot");
    $('#orgInfoTree').tree('expand',root);
    $("#add-path").dialog("open");

}
function findCurrNode(item){

    var currNode=hostList[0];
    var i=0;
    while(currNode.hostCode!=item&&i<hostList.length-1){
        currNode=hostList[++i];
    }
    // console.log("选中的hostCode"+currNode.hostCode+"选中的ip"+currNode.hostIp)
    return currNode;
}
function setDialog(title,top,left){
    $('#add-path').dialog({
        width: 400,
        height: 'auto',
        title:title,
        top:top,
        left:left,
        resizable:true,
        closed: true,
        cache: false,
        modal: true,
        shadow: false,
        // doSize:true,
        maximizable:true,
        // minimizable:true,
        maximized:false,
        toolbar: [
            // {
            //     text:'展开所有',
            //     iconCls:'icon-large-smartart',
            //     handler: function () {
            //         ExpandAll();
            //     }
            // },
            {
                text:'关闭所有',
                iconCls:'icon-large-smartart',
                handler: function () {
                    CloseapseAll();
                }
            },
        ],


    });
}
function CloseapseAll(){
    $('#orgInfoTree').tree('collapseAll');
}
//移除
// function removeit(){
//     var node = $('#orgInfoTree').tree('getSelected');
//     $('#orgInfoTree').tree('remove', node.target);
// }
function collapse(){
    var node = $('#orgInfoTree').tree('getSelected');
    $('#orgInfoTree').tree('collapse',node.target);
}
function expand(){
    var node = $('#orgInfoTree').tree('getSelected');
    $('#orgInfoTree').tree('expand',node.target);
}
function select(setPath) {
    var node = $('#orgInfoTree').tree('getSelected');
    if(node.attributes.type==="file")
    {

        setPath(node)
        $("#add-path").dialog("close")
    }
    else{
        setPath(node)
        $("#add-path").dialog("close")
    }
}
function limitHeight() {
    var obj = document.getElementById("add-path");
    var h = obj.offsetHeight;
    // console.log(h)
    if(h>=windowHeight-1/3*windowHeight)
    {
        $('#add-path').panel('resize',{
            width: 400,
            height: windowHeight-1/3*windowHeight
        });
    }
}
function showText() {//显示文件demo
    var node=$("#orgInfoTree").tree("getSelected")

    Ajax({
        url:contextPath + "/console/file/ShowTextFile",
        timeout: 0,
        data: {
            currentUrl: node.attributes.url,
            currentPath: node.attributes.path,
        },
        success: function (data) {
            if (data.code === 1){
                $("#textInfo").dialog({
                    onBeforeOpen: function () {
                        var text=data.text.replace(/\n/g,"<br\>")
                        $("#textInfoValue").html(text)

                    }
                })
                $("#textInfo").dialog("open")
            }
            else{
                apexLayer.Error(data.note);
            }
        },
        error: function (xhr, status, error) {
            layer.msg("调用服务失败");
        }
    });
}

// var exp=true;
// function action(){
//     $('#orgInfoTree').tree(exp?'collapseAll':'expandAll');
//     exp=!exp
// }
//追加节点
// function append(){
//     var t = $('#orgInfoTree');
//     var node = t.tree('getSelected');
//     t.tree('append', {
//         parent: (node?node.target:null),
//         data: [{
//             text: 'new item1'
//         },{
//             text: 'new item2'
//         }]
//     });
// }
// function ExpandAll(){
//     $('#orgInfoTree').tree('expandAll');
//
// }
